package com.effective.java.functionalobjects;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Class providing capabilities in order to merge two list of strings and sort
 * the newly created list afterwards. This class allows for usage of functional
 * objects in order to determine a customized sorting strategy.
 * 
 * @author andreas.landerer
 * 
 */
public class FunctionalObjectStrategy {

	/**
	 * Method merging and two list of strings and sorting the newly created list
	 * afterwards.
	 * 
	 * @param listA
	 *            list to be merged (mandatory)
	 * @param listB
	 *            list to be merged (mandatory)
	 * @param comparator
	 *            comparator allowing for customized sorting of the merged list
	 *            (optional)
	 * @return mergedList containing entries of both lists that were passed into
	 *         the method
	 */
	public List<String> merge(List<String> listA, List<String> listB, Comparator<String> comparator) {

		if (listA == null || listB == null) {
			throw new IllegalArgumentException("One of the mandatory input parameters 'listA' or 'listB' is NULL!");
		}

		List<String> mergedList = new ArrayList<String>(listA.size() + listB.size());
		mergedList.addAll(listA);
		mergedList.addAll(listB);
		if (comparator != null) {
			Collections.sort(mergedList, comparator);
		}
		else {
			Collections.sort(mergedList);
		}

		return mergedList;
	}
}
